package com.ssd.controller;

import com.ssd.service.ReportService;
import com.ssd.utils.DateUtil;
import com.ssd.view.BaseResponse;
import com.ssd.viewmodel.BarCharCollect;
import com.ssd.vo.SkuSeniority;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * 报表控制器类
 *
 * @title:
 * @requirement:
 * @ClassName:
 * @author:首雯静
 * @creationTime:2024-11-29/11:31
 * @version: 1.0
 */
@RestController
@RequestMapping("/manage/report")
@Slf4j
public class ReportController {

    @Resource
    private ReportService reportService;

    /**
     * 根据地区汇总销售额数据
     *
     * @param startDate 开始日期，用于筛选数据
     * @param endDate   结束日期，用于筛选数据
     * @return 销售额汇总数据
     */
    @GetMapping("/regionCollect")
    public BaseResponse<BarCharCollect> getRegionCollect(
            @RequestParam(value = "start") String startDate,
            @RequestParam(value = "end") String endDate) {
        //转换日期对象
        LocalDate start = DateUtil.parseLocalDate(startDate, "yyyy-MM-dd");
        LocalDate end = DateUtil.parseLocalDate(endDate, "yyyy-MM-dd");
        BarCharCollect result = reportService.getCollectByRegion(start, end);
        return new BaseResponse<BarCharCollect>(result);
    }

    /**
     * 获取合作商一定日期范围的收益情况
     *
     * @param partnerId
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/collectReport")
    public BaseResponse<BarCharCollect> getOwnerCollectByDate(
            @RequestParam(value = "partnerId") Integer partnerId,
            @RequestParam(value = "start") String start,
            @RequestParam(value = "end") String end) {
        //转换日期数据类型
        LocalDate startTime = DateUtil.parseLocalDate(start, "yyyy-MM-dd");
        LocalDate endTime = DateUtil.parseLocalDate(end, "yyyy-MM-dd");
        BarCharCollect result = reportService.getCollect(partnerId, startTime, endTime);
        return new BaseResponse<BarCharCollect>(result);
    }

    /**
     * 获取一定日期内的销售额趋势
     *
     * @param start 开始日期，用于筛选数据
     * @param end   结束日期，用于筛选数据
     * @return 销售额统计
     */
    @GetMapping("/amountCollect")
    public BaseResponse<BarCharCollect> getAmountCollect(
            @RequestParam(value = "start") String start,
            @RequestParam(value = "end") String end) {
        //转换日期数据类型
        LocalDate startTime = DateUtil.parseLocalDate(start, "yyyy-MM-dd");
        LocalDate endTime = DateUtil.parseLocalDate(end, "yyyy-MM-dd");
        BarCharCollect amountCollect = reportService.getAmountCollect(startTime, endTime);
        return new BaseResponse<BarCharCollect>(amountCollect);
    }

    /**
     * 指定时间段的订单总额
     *
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/totalAmount")
    public BaseResponse getTotalAmount(
            @RequestParam(value = "start") String start,
            @RequestParam(value = "end") String end) {
        //转换日期数据类型
        LocalDate startTime = DateUtil.parseLocalDate(start, "yyyy-MM-dd");
        LocalDate endTime = DateUtil.parseLocalDate(end, "yyyy-MM-dd");
        Integer totalAmount = reportService.getTotalAmount(startTime, endTime);
        double result = totalAmount / 100.0;
        String formattedResult = String.format("%.2f", result);
        return new BaseResponse(formattedResult);
    }

    /**
     * 指定时间段的订单数量
     *
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/orderCount")
    public BaseResponse getOrderCount(
            @RequestParam(value = "start") String start,
            @RequestParam(value = "end") String end) {
        //转换日期数据类型
        LocalDate startTime = DateUtil.parseLocalDate(start, "yyyy-MM-dd");
        LocalDate endTime = DateUtil.parseLocalDate(end, "yyyy-MM-dd");
        Integer orderCount = reportService.getOrderCount(startTime, endTime);
        return new BaseResponse(orderCount);
    }

    /**
     * 查询销售商品排行
     *
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/skuSeniority")
    public BaseResponse<List<SkuSeniority>> getSkuSeniority(String start, String end) {
        LocalDate startTime = DateUtil.parseLocalDate(start, "yyyy-MM-dd");
        LocalDate endTime = DateUtil.parseLocalDate(end, "yyyy-MM-dd");
        List<SkuSeniority> skuSeniority = reportService.getSkuSeniority(startTime, endTime);
        return new BaseResponse<>(skuSeniority);
    }

    /**
     * 获取进两个月的订单量
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/getOrderCollect")
    public BaseResponse<BarCharCollect> getOrderCollect(
            @RequestParam(value = "start") String start,
            @RequestParam(value = "end") String end){
        LocalDate startTime = DateUtil.parseLocalDate(start, "yyyy-MM-dd");
        LocalDate endTime = DateUtil.parseLocalDate(end, "yyyy-MM-dd");
        return new BaseResponse<>(reportService.getOrderCollect(startTime, endTime));
    }
}